package com.wtwd.campus.h5_manager.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.h5_manager.dao.DeviceLogMapper;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.entity.MessageTypeListVo;
import com.wtwd.campus.h5_manager.entity.MessageTypeVo;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-03-14
 */
@Service
public class DeviceLogServiceImpl implements DeviceLogService {

    @Autowired
    private DeviceLogMapper deviceLogMapper;

    @Override
    public Integer addDeviceLog(DeviceLog deviceLog) {
        Integer rest = 0;
        String imei = deviceLog.getImei();
        String index = imei.substring(imei.length() - 1);
        switch (index) {
            case "0":
                rest = deviceLogMapper.addDevice0(deviceLog);
                break;
            case "1":
                rest = deviceLogMapper.addDevice1(deviceLog);
                break;
            case "2":
                rest = deviceLogMapper.addDevice2(deviceLog);
                break;
            case "3":
                rest = deviceLogMapper.addDevice3(deviceLog);
                break;
            case "4":
                rest = deviceLogMapper.addDevice4(deviceLog);
                break;
            case "5":
                rest = deviceLogMapper.addDevice5(deviceLog);
                break;
            case "6":
                rest = deviceLogMapper.addDevice6(deviceLog);
                break;
            case "7":
                rest = deviceLogMapper.addDevice7(deviceLog);
                break;
            case "8":
                rest = deviceLogMapper.addDevice8(deviceLog);
                break;
            case "9":
                rest = deviceLogMapper.addDevice9(deviceLog);
                break;
        }
        return rest;
    }


    @Override
    public IPage<DeviceLog> queryDeviceLogConditions(Long startTime, Long endTime, String imei, String massageType, String type, Page<DeviceLog> page) {
        String index = imei.substring(imei.length() - 1);
        IPage<DeviceLog> deviceLogIPage = null;
        switch (index) {
            case "0":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions0(startTime, endTime, imei, massageType, type, page);
                break;
            case "1":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions1(startTime, endTime, imei, massageType, type, page);
                break;
            case "2":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions2(startTime, endTime, imei, massageType, type, page);
                break;
            case "3":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions3(startTime, endTime, imei, massageType, type, page);
                break;
            case "4":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions4(startTime, endTime, imei, massageType, type, page);
                break;
            case "5":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions5(startTime, endTime, imei, massageType, type, page);
                break;
            case "6":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions6(startTime, endTime, imei, massageType, type, page);
                break;
            case "7":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions7(startTime, endTime, imei, massageType, type, page);
                break;
            case "8":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions8(startTime, endTime, imei, massageType, type, page);
                break;
            case "9":
                deviceLogIPage = deviceLogMapper.queryDeviceLogConditions9(startTime, endTime, imei, massageType, type, page);
                break;
        }
        ClassPathResource resource = new ClassPathResource("command.json");
        JSONObject jsonData = null;
        if (resource.exists()) {
            try {
                jsonData = (JSONObject) JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, JSONObject.class,
                        // 自动关闭流
                        Feature.AutoCloseSource,
                        // 允许注释
                        Feature.AllowComment,
                        // 允许单引号
                        Feature.AllowSingleQuotes,
                        Feature.OrderedField
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        List<DeviceLog> records = deviceLogIPage.getRecords();
        JSONObject messageType = (JSONObject) jsonData.get("messageType");
        Map messageTypeMap = (Map) JSONObject.toJavaObject(messageType, Map.class);
        JSONObject typeJson = (JSONObject) jsonData.get("type");
        Map typeMap = (Map) JSONObject.toJavaObject(typeJson, Map.class);
        typeMap.putAll(messageTypeMap);
        records.stream().forEach(
                e->{
                    e.setBigType((String)typeMap.get(e.getBigType()));
                    e.setType((String)typeMap.get(e.getType()));
                }
        );
        return deviceLogIPage;
    }

    @Override
    public MessageTypeListVo getDeviceMessageType(String type) {
        ClassPathResource resource = new ClassPathResource("command.json");
        JSONObject jsonData = null;
        if (resource.exists()) {
            try {
                jsonData = (JSONObject) JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, JSONObject.class,
                        // 自动关闭流
                        Feature.AutoCloseSource,
                        // 允许注释
                        Feature.AllowComment,
                        // 允许单引号
                        Feature.AllowSingleQuotes,
                        Feature.OrderedField
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        JSONObject rest = null;
        if ("0".equals(type)) {
            rest = jsonData;
        } else {
            rest = (JSONObject) jsonData.get(type);
        }
        MessageTypeListVo messageVos = new MessageTypeListVo();
        Map<String, Object> map = rest.toJavaObject(Map.class);
        Map<String, String> messageType = (Map) map.get("messageType");
        List<MessageTypeVo> messageTypeVos = new ArrayList<>();
        Iterator<Map.Entry<String, String>> it = messageType.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> entry = it.next();
            MessageTypeVo messageTypeVo = new MessageTypeVo();
            messageTypeVo.setLable(entry.getValue());
            messageTypeVo.setValue(entry.getKey());
            messageTypeVos.add(messageTypeVo);
        }
        messageVos.setMessageTypeVos(messageTypeVos);

        Map<String, String> typeMap = (Map) map.get("type");
        List<MessageTypeVo> typeVos = new ArrayList<>();
        Iterator<Map.Entry<String, String>> typeIt = typeMap.entrySet().iterator();
        while(typeIt.hasNext()){
            Map.Entry<String, String> entry = typeIt.next();
            MessageTypeVo typeVo = new MessageTypeVo();
            typeVo.setLable(entry.getValue());
            typeVo.setValue(entry.getKey());
            typeVos.add(typeVo);
        }
        messageVos.setTypeVos(typeVos);
        return messageVos;
    }

    @Override
    public Integer deleteDeviceLogByTime(long time) {
        Integer rest=0;
        rest+= deviceLogMapper.deleteDeviceLog0ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog1ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog2ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog3ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog4ByTime(time);
        rest+= deviceLogMapper.deleteDeviceLog5ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog6ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog7ByTime(time);
        rest+=deviceLogMapper.deleteDeviceLog8ByTime(time);
        rest+= deviceLogMapper.deleteDeviceLog9ByTime(time);
        return rest;
    }
}
